﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainPositionQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Vars
        public MstPositionEntity SelectedPosition { get; set; }
        #endregion

        #region Ctor
        public frmMaintainPositionQuery()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComobox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbDeleteFlag, typeof(DeleteFlag), typeof(string), true);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colDeleteFlag, typeof(DeleteFlag), typeof(int), true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbPositionLevel, "PLEVEL", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colLevel, "PLEVEL", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtPositionCode.Text) == false)
            {
                condition += " AND POSITION_CODE LIKE '%" + txtPositionCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtPositionName.Text) == false)
            {
                condition += " AND POSITION_NAME LIKE '%" + txtPositionName.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbDeleteFlag.Text) == false && cmbDeleteFlag.SelectedValue != null)
            {
                condition += " AND DELETE_FLAG = " + cmbDeleteFlag.SelectedValue.ToString() + "";
            }
            if (string.IsNullOrEmpty(cmbPositionLevel.Text) == false && cmbPositionLevel.SelectedValue != null)
            {
                condition += " AND LEVEL = '" + cmbPositionLevel.SelectedValue.ToString() + "'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "POSITION_CODE";
            DataTable dt = MstPositionBs.GetPositionByCondition(condition, orderBy, "");
            gvMain.DataSource = dt;
        }
        #endregion

        #region Events
        private void frmMaintainPositionQuery_Load(object sender, EventArgs e)
        {
            BindComobox();
            BindDataGrid();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (SelectedPosition == null)
            {
                MessageBox.Show("请选择一个职位！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr=gridView1.GetDataRow(e.RowHandle);
            SelectedPosition = new MstPositionEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (SelectedPosition != null)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        #endregion
    }
}
